package com.atguigu.juc.date928;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: shenhao
 * @CreatTime: 2024/9/28 20:13
 * @Version: 1.0
 * 顺序唤醒,每个线程,共有三个线程, A独立打印5次,B独立打印10次,C独立打印15次,然后循环10轮
 * 这个写法不太对, 应该使用while进行判断, 防止虚假唤醒, 写法不对
 * 判断, 干活 , 通知, 标志位 判断 , 通过多个 condition1 进行定制话通信
 */
public class SortSignal {
    public static void main(String[] args) {
        PrintNum printNum = new PrintNum();
        Thread thread1 = new Thread(() -> {
            for (int result = 0; result < 10; result++) {
                printNum.print5();
            }
        }, "A");
        Thread thread2 = new Thread(() -> {
            for (int result = 0; result < 10; result++) {
                printNum.print10();
            }
        }, "B");
        Thread thread3 = new Thread(() -> {
            for (int result = 0; result < 10; result++) {
                printNum.print15();
                System.out.println("轮次"+(result+1));
            }
        }, "C");
        thread1.start();
        thread2.start();
        thread3.start();

    }
}

class PrintNum {
    Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();
    // 标志位  0 A 1 B 2 C
    int flag = 0;

    public void print5() {
        lock.lock();
        try {
            // 错误写法
            /*if (flag == 0) {
                for (int i = 1; i <= 5; i++) {
                    System.out.println(Thread.currentThread().getName() + "打印: " + i);
                }
                flag = 1;
                condition2.signal();
            } else {
                condition1.await();
            }*/
            while (flag != 0) {
                condition1.await();
            }
            for (int i = 1; i <= 5; i++) {
                System.out.println(Thread.currentThread().getName() + "打印: " + i);
            }
            flag = 1;
            condition2.signal();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    public void print10() {
        lock.lock();
        try {
            /*if (flag == 1) {
                for (int i = 1; i <= 10; i++) {
                    System.out.println(Thread.currentThread().getName() + "打印: " + i);
                }
                flag = 2;
                condition3.signal();
            } else {
                condition2.await();
            }*/
            while (flag != 1) {
                condition2.await();
            }
            for (int i = 1; i <= 10; i++) {
                System.out.println(Thread.currentThread().getName() + "打印: " + i);
            }
            flag = 2;
            condition3.signal();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    public void print15() {
        lock.lock();
        try {
            /*if (flag == 2) {
                for (int i = 1; i <= 15; i++) {
                    System.out.println(Thread.currentThread().getName() + "打印: " + i);
                }
                flag = 0;
                condition1.signal();
            } else {
                condition3.await();
            }*/
            // 判断, 干活, 通知, 顺序不能错
            while (flag != 2) {
                condition3.await();
            }
            for (int i = 1; i <= 15; i++) {
                System.out.println(Thread.currentThread().getName() + "打印: " + i);
            }
            flag = 0;
            condition1.signal();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

}
